/**
*  \file kwpserver.h
*
*  \brief
*
*  \author Michael J. Hohmann  michael.hohmann@linde-mh.de
*
*
*
*
*
*/
#ifndef KWPSERVER_H_INCLUDED
#define KWPSERVER_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"
#include "../kwptprx/kwptprx.h"
#include "../kwptptx/kwptptx.h"
#include "../kwp/kwp_sid.h"
/*================================================[ public defines   ]================================================*/

/*================================================[ public datatypes ]================================================*/

typedef void ( *kwpserver_transmit_func_t )( U8 *data );

/** Function pointer to a kwp service */
typedef enum kwp_response_code (*kwpserver_service_function_t)( struct kwp_msg* msg );

/** */
struct kwpserver_services
{
   enum kwp_service_identifier sid;
   kwpserver_service_function_t  service_func;
};


enum kwpserver_state
{
   kwpserver_state_receiving,
   kwpserver_state_pending,
   kwpserver_state_transmitting
};


/** Object definition.
* 
* The struct kwpserver_obj defines the attribute of an object.
*/
struct kwpserver_obj
{	
   struct kwpclient_services const* services;
   kwpserver_transmit_func_t transmit;
   //void *p_tx_obj;
   canmsg_id_t tx_id;
   U8 block_size;
   U8 separation_time;
   U16 timeout;
   U16 pending_time;
   U16 time_base;

   canmsg_t flow_control_msg;
   struct kwpserver_services const*service_table;
   U8 number_of_services;
   enum kwpserver_state state;
   struct kwptprx_obj rx_obj;
   struct kwptptx_obj tx_obj;
   struct kwp_msg buffer;

   U32 pending_timer;
};

/*================================================[ public variables ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/

/**
*  Constructor.
*
*  Construct an object. 
*
*  \param me Object pointer. NIL will throw an assert.
*
*  \return RC_SUCCESS if successful.
*/
RC kwpserver_construct( struct kwpserver_obj *me ,struct kwpserver_services const* services, kwpserver_transmit_func_t tx_func, U8 block_size, U8 seperation_time, U16 timeout, U16 pending_time );
void kwpserver_process_msg(struct kwpserver_obj *me, U8 *data );
void kwpserver_cyclic_service(struct kwpserver_obj *me);
void kwpserver_idle_service(struct kwpserver_obj *me);

/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2020 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
